// 从index.html提取的内联JavaScript代码

// 页面加载检测脚本
document.addEventListener('DOMContentLoaded', function() {
    // 隐藏预加载提示
    setTimeout(function() {
        var preloadMessage = document.getElementById('preload-message');
        if (preloadMessage) {
            preloadMessage.style.opacity = '0';
            preloadMessage.style.transition = 'opacity 0.5s';
            setTimeout(function() {
                preloadMessage.style.display = 'none';
            }, 500);
        }
    }, 500);
    

});

// ECharts降采样插件 - LTTB算法实现
(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
    typeof define === 'function' && define.amd ? define(['exports'], factory) :
    (global = global || self, factory(global.echarts = global.echarts || {}));
}(this, (function (exports) { 
    'use strict';
    
    var LTTBDownsampler = {
        name: 'lttb-downsampler',
        // Largest-Triangle-Three-Buckets算法
        downsample: function(data, threshold) {
            var dataLength = data.length;
            if (threshold >= dataLength || threshold === 0) {
                return data; // 如果阈值大于数据长度，直接返回原数据
            }

            var sampled = [];
            var bucketSize = (dataLength - 2) / (threshold - 2); // 计算桶大小
            var a = 0, nextA = 0;
            var maxAreaPoint, maxArea, area, i;

            // 添加第一个点
            sampled.push(data[0]);

            for (var i = 0; i < threshold - 2; i++) {
                // 计算当前桶的范围
                var avgX = 0, avgY = 0;
                var avgRangeStart = Math.floor((i + 1) * bucketSize) + 1;
                var avgRangeEnd = Math.floor((i + 2) * bucketSize) + 1;
                avgRangeEnd = avgRangeEnd < dataLength ? avgRangeEnd : dataLength;

                var avgRangeLength = avgRangeEnd - avgRangeStart;

                // 计算下一个桶的平均点
                for (; avgRangeStart < avgRangeEnd; avgRangeStart++) {
                    avgX += data[avgRangeStart][0] * 1; // 确保是数字
                    avgY += data[avgRangeStart][1] * 1;
                }
                avgX /= avgRangeLength;
                avgY /= avgRangeLength;

                // 计算点a和平均点之间的三角形面积
                var rangeOffs = Math.floor((i + 0) * bucketSize) + 1;
                var rangeTo = Math.floor((i + 1) * bucketSize) + 1;

                var pointAX = data[a][0] * 1; // 确保是数字
                var pointAY = data[a][1] * 1;

                maxArea = -1;

                for (; rangeOffs < rangeTo; rangeOffs++) {
                    // 计算三角形面积
                    area = Math.abs(
                        (pointAX - avgX) * (data[rangeOffs][1] - pointAY) -
                        (pointAX - data[rangeOffs][0]) * (avgY - pointAY)
                    ) * 0.5;

                    if (area > maxArea) {
                        maxArea = area;
                        maxAreaPoint = data[rangeOffs];
                        nextA = rangeOffs; // 更新下一个a点
                    }
                }

                sampled.push(maxAreaPoint); // 添加面积最大的点
                a = nextA; // 更新a点
            }

            // 添加最后一个点
            sampled.push(data[dataLength - 1]);

            return sampled;
        }
    };

    // 注册到echarts
    if (typeof exports.registerProcessor === 'function') {
        exports.registerProcessor(function(ecModel, api) {
            ecModel.eachSeriesByType('line', function(seriesModel) {
                var data = seriesModel.getData();
                var sampling = seriesModel.get('sampling');
                var threshold = seriesModel.get('largeThreshold') || 200;
                
                if (sampling === 'lttb' && data.count() > threshold) {
                    var rawData = data.getValues(['x', 'y'], true);
                    var points = [];
                    
                    for (var i = 0; i < rawData[0].length; i++) {
                        points.push([rawData[0][i], rawData[1][i]]);
                    }
                    
                    var sampled = LTTBDownsampler.downsample(points, threshold);
                    var newData = [];
                    
                    for (var i = 0; i < sampled.length; i++) {
                        newData.push([sampled[i][0], sampled[i][1]]);
                    }
                    
                    seriesModel.setData(newData);
                }
            });
        });
    }
    
    // 导出
    exports.LTTBDownsampler = LTTBDownsampler;
})));

 